<template>
  <div class="page">
    <!--查询区域-->
    <div class="jeecg-basic-table-form-container">
      <a-form
        @finish="searchQuery"
        :model="queryParam"
        :label-col="{
          style: { width: '70px' },
        }"
        :wrapper-col="{
          style: { width: '150px' },
        }"
        style="display: flex"
      >
        <a-form-item label="客户名称" style="margin-bottom: 8px">
          <a-input placeholder="请输入客户名称" v-model:value="queryParam.company" />
        </a-form-item>
        <a-button class="search-btn" type="primary" html-type="submit">查询</a-button>
      </a-form>
    </div>
    <!--引用表格-->
    <BasicTable @register="registerTable">
      <!--插槽:table标题-->
      <template #tableTitle>
        <a-button type="primary" @click="handleAdd" preIcon="ant-design:plus-outlined"> 新增</a-button>
        <a-button type="primary" preIcon="ant-design:export-outlined" @click="onExportXls"> 导出</a-button>
      </template>
      <!--操作栏-->
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" />
      </template>
    </BasicTable>
    <!-- 表单区域 -->
    <SalesmanCustomerConfigModal ref="registerModal" @success="handleSuccess" />
  </div>
</template>

<script lang="ts" name="settlement-salesman-customer" setup>
  import { ref } from 'vue';
  const { handleExportXls } = useMethods();
  import { BasicTable, TableAction } from '/@/components/Table';
  import { useListPage } from '/@/hooks/system/useListPage';
  import { columns } from './SalesmanCustomerConfig.data';
  import { useRouter } from 'vue-router';
  import { list, deleteOne } from './SalesmanCustomerConfig.api';
  import SalesmanCustomerConfigModal from './components/SalesmanCustomerConfigModal.vue';
  import {exportSalesmanManagerUrl} from "/@/views/settlement/salesmanCustomerConfig/SalesmanCustomerConfig.api";
  import {filterObj} from "/@/utils/common/compUtils";
  import {useMethods} from "/@/hooks/system/useMethods";

  const router = useRouter();
  const queryParam = ref<any>({});
  const registerModal = ref();
  //注册table数据
  const { tableContext } = useListPage({
    tableProps: {
      title: 'insurance_settlement_config',
      api: list,
      columns,
      canResize: false,
      useSearchForm: false,
      actionColumn: {
        width: 120,
        fixed: 'right',
      },
      beforeFetch: (params) => {
        console.log('router.currentRoute.value.query', router.currentRoute.value.query);
        return Object.assign(params, queryParam.value, router.currentRoute.value.query);
      },
    },
  });
  const [registerTable, { reload }] = tableContext;

  /**
   * 新增事件
   */
  function handleAdd() {
    registerModal.value.disableSubmit = false;
    registerModal.value.add({ ...router.currentRoute.value.query });
  }

  /**
   * 编辑事件
   */
  function handleEdit(record: Recordable) {
    registerModal.value.disableSubmit = false;
    registerModal.value.edit({ ...record, ...router.currentRoute.value.query });
  }

  /**
   * 详情
   */
  function handleDetail(record: Recordable) {
    registerModal.value.disableSubmit = true;
    registerModal.value.edit(record);
  }

  function onExportXls() {
    let paramsForm = Object.assign({}, queryParam.value, router.currentRoute.value.query);
    console.info(paramsForm);
    return handleExportXls("客户列表", exportSalesmanManagerUrl, filterObj(paramsForm));
  }


  /**
   * 删除事件
   */
  async function handleDelete(record) {
    await deleteOne({ id: record.id }, handleSuccess);
  }
  /**
   * 查询
   */
  function searchQuery() {
    reload();
  }

  function handleSuccess() {
    reload();
  }

  /**
   * 操作栏
   */
  function getTableAction(record) {
    return [
      {
        label: '编辑',
        onClick: handleEdit.bind(null, record),
      },
      {
        label: '删除',
        onClick: handleDelete.bind(null, record),
      },
      {
        label: '详情',
        onClick: handleDetail.bind(null, record),
      },
    ];
  }
</script>

<style lang="less" scoped>
  .jeecg-basic-table-form-container {
    background-color: #fff;
    padding: 10px 10px 0px 0px;
    margin-bottom: 10px;
  }
  .table-page-search-submitButtons {
    display: block;
    margin-bottom: 24px;
    white-space: nowrap;
  }
  .search-btn {
    margin-left: 10px;
  }

  .page {
    padding: 10px;
  }
</style>
